package com.moly.grpc.nio;

import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.Set;

public class FileClient {
    public static void main(String[] args) {

        FileClient client = new FileClient();
        client.init();
        client.connect();

    }

    private SocketChannel client = null;
    private Selector selector = null;

    public void regisetrChannel(Selector selector, SelectableChannel channel, int ops) {
        try {
            channel.configureBlocking(false);
            channel.register(selector, ops);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void init() {
        try {
            client = SocketChannel.open();
            client.configureBlocking(false);
            selector = Selector.open();
            regisetrChannel(selector, client, SelectionKey.OP_CONNECT);
            client.connect(new InetSocketAddress("localhost", 8899));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void connect() {
        while (true) {
            try {
                selector.select();
                Set<SelectionKey> selectionKeys = selector.selectedKeys();

                Iterator<SelectionKey> iterator = selectionKeys.iterator();
                while (iterator.hasNext()) {
                    SelectionKey next = iterator.next();
                    handle(next);
                    iterator.remove();
                }


            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void handle(SelectionKey key) {
        if (key.isConnectable()) {
            SocketChannel channel = (SocketChannel) key.channel();
            try {
                channel.finishConnect();

                regisetrChannel(selector, channel, SelectionKey.OP_READ);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        if (key.isReadable()) {

            SocketChannel channel = (SocketChannel) key.channel();

            int len = 0;
            ByteBuffer buffer = ByteBuffer.allocate(100);

            String s = "";
            try {
                while (((len = channel.read(buffer)) > 0)) {

                    buffer.flip();
                    s += new String(buffer.array(),0,len, Charset.forName("utf-8"));

                    buffer.clear();

                }
                if (len < 0) {
                    channel.close();
                }
                System.out.println("服务器 发送数据 :" +s);
                regisetrChannel(selector, channel, SelectionKey.OP_WRITE);
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        if (key.isWritable()) {

            SocketChannel channel = (SocketChannel) key.channel();

            String s = "";
            System.out.println("请输入");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in);


            try {
                char[] a = new char[1024];
                int read = inputStreamReader.read(a);
                String s1 = new String(a, 0, read);
                byte[] bytes = s1.getBytes(Charset.forName("utf-8"));
                channel.write(ByteBuffer.wrap(bytes));
                regisetrChannel(selector, channel, SelectionKey.OP_READ);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
